package com.hb.common.aspect;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import com.hb.common.exception.DbBackupException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;


/**
 * 还原
 */
@Aspect
@Component
public class DbRestoreAspect {

    private static final Logger log = LoggerFactory.getLogger(DbRestoreAspect.class);

    @Around(value = "@annotation(com.hb.common.aspect.DbRestore)")
    public Object doAroundAdvice(ProceedingJoinPoint joinPoint) throws DbBackupException {
        Object proceed;
        TimeInterval timer = DateUtil.timer();
        // 获取参数
        Object[] args = joinPoint.getArgs();
        try {
            log.info("开始还原数据库");
            proceed = joinPoint.proceed();
            log.info("数据库还原成功，耗时{}毫秒", timer.interval());
        } catch (Throwable e) {
            log.error("数据库还原失败，原因:{}", e.getMessage());
            throw new DbBackupException(e.getMessage());
        }
        return proceed;
    }

}

